package com.fallsvc.re.javasesets.dstackandqueue;

import java.util.Stack;

/**
 * @auther falls_vc
 * description:
 * @date 2025/10/18  15:26
 */
public class Demo2 {
    public static boolean isValid(String s) {
        char[] str = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (char ch : str) {
            if (isLeft(ch)) {
                stack.push(ch);
            }else if(isRight(ch)){
                if(stack.isEmpty() || !match(stack.peek(),ch)){
                    return false;
                }
                stack.pop();
            }
        }
        if(!stack.isEmpty()) return false;
        return true;
    }
    public static boolean isLeft(char ch) {
        return '(' == ch || '[' == ch || '{' == ch;
    }
    public static boolean isRight(char ch) {
        return ')' == ch || ']' == ch || '}' == ch;
    }

    public static boolean match(char x,char y) {
        return ('(' == x)&&(')' == y) ||
                ('[' == x)&&(']' == y) ||
                ('{' == x)&&('}' == y);
    }
    public static void main(String[] args) {
        String a = "()";
        isValid(a);
    }
}
